package com.ruoyi.sports.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sports.domain.Event;
import com.ruoyi.sports.domain.SportsMeeting;
import com.ruoyi.sports.domain.vo.ApplicationVO;
import com.ruoyi.sports.domain.vo.EventVO;
import com.ruoyi.sports.mapper.EventMapper;
import com.ruoyi.sports.mapper.SportsMeetingMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sports.mapper.ApplicationMapper;
import com.ruoyi.sports.domain.Application;
import com.ruoyi.sports.service.IApplicationService;

/**
 * 报名参赛记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-08
 */
@Service
public class ApplicationServiceImpl implements IApplicationService 
{
    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private EventMapper eventMapper;

    @Autowired
    private SportsMeetingMapper sportsMeetingMapper;



    /**
     * 查询报名参赛记录
     * 
     * @param applicationId 报名参赛记录主键
     * @return 报名参赛记录
     */
    @Override
    public ApplicationVO selectApplicationByApplicationId(Long applicationId)
    {
        Application application = applicationMapper.selectApplicationByApplicationId(applicationId);
        ApplicationVO VO=new ApplicationVO();
        BeanUtils.copyProperties(application,VO);
        VO.setOldStatus(application.getStatus());
        return VO;
    }

    /**
     * 查询报名参赛记录列表
     * 
     * @param application 报名参赛记录
     * @return 报名参赛记录
     */
    @Override
    public List<Application> selectApplicationList(Application application)
    {
        return applicationMapper.selectApplicationList(application);
    }

    /**
     * 新增报名参赛记录
     * 
     * @param application 报名参赛记录
     * @return 结果
     */
    @Override
    public int insertApplication(Application application)
    {
        return applicationMapper.insertApplication(application);
    }

    /**
     * 修改报名参赛记录
     * 
     * @param application 报名参赛记录
     * @return 结果
     */
    @Override
    public int updateApplication(ApplicationVO application)
    {
        if(application.getOldStatus()==0||application.getOldStatus()==2){
            switch (application.getStatus()){
                case 2:
                case 0: return applicationMapper.updateApplication(application);
                case 1:
                    Event event = eventMapper.selectEventByEventId(application.getEventId());
                    if(event.getNowNumber()+1>event.getMaxNumber()){
                        return 0;
                    }else{
                        event.setNowNumber(event.getNowNumber()+1);
                        eventMapper.updateEvent(event);
                    }
            }
        }else {
            switch (application.getStatus()){
                case 1: return applicationMapper.updateApplication(application);
                case 0:
                case 2:
                    Event event = eventMapper.selectEventByEventId(application.getEventId());
                    if(event.getNowNumber()-1<0){
                        return 0;
                    }else{
                        event.setNowNumber(event.getNowNumber()-1);
                        eventMapper.updateEvent(event);
                    }
            }
        }
        return applicationMapper.updateApplication(application);
    }

    /**
     * 批量删除报名参赛记录
     * 
     * @param applicationIds 需要删除的报名参赛记录主键
     * @return 结果
     */
    @Override
    public int deleteApplicationByApplicationIds(Long[] applicationIds)
    {
        for (Long applicationId:applicationIds){
            Application application = this.selectApplicationByApplicationId(applicationId);
            if (application.getStatus()==1){
                Event event = eventMapper.selectEventByEventId(application.getEventId());
                event.setNowNumber(event.getNowNumber()-1);
                eventMapper.updateEvent(event);
            }
        }
        return applicationMapper.deleteApplicationByApplicationIds(applicationIds);
    }

    /**
     * 删除报名参赛记录信息
     * 
     * @param applicationId 报名参赛记录主键
     * @return 结果
     */
    @Override
    public int deleteApplicationByApplicationId(Long applicationId)
    {
        Application application = this.selectApplicationByApplicationId(applicationId);
        if (application.getStatus()==1){
            Event event = eventMapper.selectEventByEventId(application.getEventId());
            event.setNowNumber(event.getNowNumber()-1);
            eventMapper.updateEvent(event);
        }
        return applicationMapper.deleteApplicationByApplicationId(applicationId);
    }

    /**
     * 用户报名参赛
     *
     * @param event
     * @return
     */
    @Override
    public int enroll(EventVO event) {
        Date applicaitonEnd = event.getApplicaitonEnd();
        Date applicaitonStart = event.getApplicaitonStart();
        Date now =new Date();
        if (now.before(applicaitonEnd)&&now.after(applicaitonStart)){
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Long userId = loginUser.getUserId();
            String nickName = loginUser.getUser().getNickName();
            Application application=new Application();
            application.setUserId(userId);
            application.setNickName(nickName);
            application.setEventId(event.getEventId());
            application.setStatus(0);
            application.setEventName(event.getEventName());
            return applicationMapper.insertApplication(application);
        }else{
            return 0;
        }

    }

    /**
     * 查询当前登录用户的比赛报名记录
     *
     * @param application
     * @return
     */
    @Override
    public List<ApplicationVO> selectMyApplicationList(Application application) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        application.setUserId(userId);
        List<Application> applications = applicationMapper.selectApplicationList(application);
        List<ApplicationVO> list=new ArrayList<>();
        for (Application a:applications){
            Event event = eventMapper.selectEventByEventId(a.getEventId());
            SportsMeeting sportsMeeting = sportsMeetingMapper.selectSportsMeetingBySportsMeetingId(event.getSportsMeetingId());
            ApplicationVO VO=new ApplicationVO();
            BeanUtils.copyProperties(a,VO);
            VO.setSportsMeetingName(sportsMeeting.getSportsMeetingName());
            list.add(VO);
        }
        return list;
    }

    @Override
    public int cancelApplicationByApplicationId(Long applicationId) {
        Application application = this.selectApplicationByApplicationId(applicationId);

        Date applicaitonEnd = sportsMeetingMapper.selectSportsMeetingBySportsMeetingId(
                eventMapper.selectEventByEventId(
                        application.getEventId()).getSportsMeetingId()
        ).getApplicaitonEnd();
        Date now =new Date();
        if (now.after(applicaitonEnd)){
            return 0;
        }
        if (application.getStatus()==1){
            Event event = eventMapper.selectEventByEventId(application.getEventId());
            event.setNowNumber(event.getNowNumber()-1);
            eventMapper.updateEvent(event);
        }
        return applicationMapper.deleteApplicationByApplicationId(applicationId);
    }
}
